<template>
	<view>
		<back :showBackText="false" customClass="bg-gradual-blue text-white" title="选择课程"></back>
		<view class="flex flex-direction" style="min-height: 100vh;" v-if="isLoad">
			<view class="flex-sub" style="margin-bottom: 270rpx;">
				<view
					:style="'padding-top:0.1rpx; padding-bottom:' + (isAllScreen ? 68 : 20) + 'rpx;padding-left:30rpx;padding-right:30rpx;'">
					<block v-for="(item, index) in listData" :key="index">
						<view class="group-head">
							<image
								:src="index == 0 ? '/static/img/ic_course_ybk.png' : '/static/img/ic_course_wbk.png'">
							</image>
							<text>{{ item.group }}</text>
						</view>
						<view @tap="courseTap" class="course-layout" :data-child="idx" :data-index="index"
							v-for="(course, idx) in item.courses" :key="idx">
							<view style="flex: 1">
								<view class="course-name">{{ course.name }}</view>
								<courselabel :code="course.code" :courseApply="index == 0 ? '已报考' : '未报考'">
								</courselabel>
							</view>
							<view class="">							<text @tap.stop.prevent="editTap" :data-item="course" 
								:class="['cuIcon-' + (course.joined == 1 ? 'pulldown' : 'pullup'), 'course-icon']">
							</text>
							</view>

						</view>
					</block>
					<view class="wbk_hint">点击课程右边向上箭头添加报考</view>
				</view>
			</view>
			<adfootbanner></adfootbanner>
		</view>
	</view>
</template>

<script>
	import {
		get
	} from "@/common/js/http.js";
	let app = getApp();
	let that = null;
	export default {
		data() {
			return {
				isAllScreen: app.globalData.isAllScreen,
				listData: [],
				mainType: 0,
				isLoad: false,
			};
		},
		onLoad(o) {
			that = this;
			that.mainType = o.mainType;
			that.getCourse();
		},
		methods: {
			getCourse() {
				let currenProfession = app.globalData.config.storage.getCurrentProfessionData();
				app.globalData.server
					.getRequest('v2/getCourses', {
						professions_id: currenProfession.id
					})
					.then(function(res) {
						that.listData = res.data;
						that.isLoad = true;
					})
					.catch(function(e) {
						app.showToast('获取课程列表失败');
					});
			},
			editTap(e) {
				if (app.globalData.isLogin) {
					let o = e.currentTarget.dataset.item;
					console.log(e);
					app.globalData.server
						.postRequest('v2/joinCourses', {
							course_id: o.id,
							join: o.joined
						})
						.then(function(e) {
							that.getCourse();
						})
						.catch(function(e) {
							console.log(e);
							app.showToast('编辑失败');
						});
				} else {
					app.showToast('未登录');
				}
			},
			async courseTap(options) {
				let item = that.listData[options.currentTarget.dataset.index].courses[options.currentTarget.dataset
					.child];
        let url = '../course/detail?title=' + item.name + '&id=' + item.id;
				uni.navigateTo({
					url: url
				});
			},
		}
	};
</script>
<style src="./course.css"></style>